package team.pra.manager.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageHelper;
import team.pra.manager.pojo.User;
import team.pra.manager.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author reader-l
 * 用户管理的CURD
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @RequiresRoles({"vip"})
    public PageHelper userList(String token, PageHelper<User> pageHelper,
                               @RequestParam(name = "search[value]") String searchvalue,
                               @RequestParam(name = "order[0][column]") Integer orderColunm,
                               @RequestParam(name = "order[0][dir]") String orderDir,
                               HttpServletRequest request){
        String columnName = request.getParameter("columns["+orderColunm+"][data]");
        pageHelper.setColumnName(columnName);
        pageHelper.setSearchValue(searchvalue);
        pageHelper.setOrderDir(orderDir);
        System.out.println(pageHelper);
        userService.findUserByCondition(pageHelper);
        return pageHelper;
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @RequiresPermissions({"insert"})
    public ManagerResult addUser(String token,User user){
        return userService.insertoToUser(user);
    }

    @GetMapping(value = "/delete")
    @RequiresPermissions({"delete"})
    public ManagerResult deleteUser(String token,Integer id){
        return userService.deleteUser(id);
    }


    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @RequiresPermissions({"update"})
    public ManagerResult updateUser(String token,User user){
        return userService.updateUser(user);
    }
}
